/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;

/**
 *
 * @author Alberto, Alessandra
 */
@Entity
@NamedQueries({
    @NamedQuery(
        name = "findByNameAndNation",
        query = "SELECT c FROM City c WHERE c.name = :Cname AND c.nation = :Cnation"
    ),
    @NamedQuery(
        name = "findAllByNation",
        query = "SELECT c FROM City c WHERE c.nation = :Cnation"
    )
})

public class City implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    
    @NotNull(message = "May not be empty")
    private String name;
    
    @OneToMany(mappedBy = "city")
    private Collection<Place> places;
    
    @ManyToOne
    @JoinColumn(name = "idnation", referencedColumnName = "id")
    private Nation nation;

    public City(String name, Nation nation) {
        this.places = new ArrayList<Place>();
        this.name = name;
        this.nation = nation;
    }
    
    public City () {
        
    }

    public Collection<Place> getPlaces() {
        return places;
    }

    public void setPlaces(Collection<Place> places) {
        this.places = places;
    }

    public Nation getNation() {
        return nation;
    }

    public void setNation(Nation nation) {
        this.nation = nation;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    
    public void addPlace (Place place) {
        this.places.add(place);
    }
    
    public void removePlace (Place place) {
        this.places.remove(place);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final City other = (City) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        return true;
    }

}
